/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.daydream.treeFlatFormat.json;

import org.apache.commons.lang3.StringUtils;
import org.pajamas.protocol.TreeFlatFormatter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.Map;

/**
 * @author william
 * @since 2024/1/5
 */

public abstract class AbstractJsonTreeFlatFormatter implements TreeFlatFormatter {
    @Override
    public Map<String, Object> format(Object srcObj) {
        if (srcObj == null) {
            return Collections.emptyMap();
        }
        return this.doFormat(srcObj);
    }

    @Override
    public Map<String, Object> format(String text) {
        if (StringUtils.isBlank(text)) {
            return Collections.emptyMap();
        }
        return this.doFormat(text);
    }

    @Override
    public Map<String, Object> format(File file) {
        if (file == null || !file.exists() || !file.isFile()) {
            return Collections.emptyMap();
        }
        return this.doFormat(file);
    }

    @Override
    public Map<String, Object> format(InputStream inputStream) {
        if (inputStream == null) {
            return Collections.emptyMap();
        }
        return this.doFormat(inputStream);
    }

    @Override
    public Map<String, Object> format(URL url) {
        if (url == null) {
            return Collections.emptyMap();
        }
        return this.doFormat(url);
    }

    public Map<String, Object> format(Resource resource) {
        if (resource == null || !resource.exists() || !resource.isReadable()) {
            return Collections.emptyMap();
        }
        try {
            if (resource instanceof FileSystemResource) {
                return this.format(resource.getFile());
            }
            if (resource instanceof UrlResource) {
                return this.format(resource.getURL());
            }
        } catch (IOException ex) {
            throw new IllegalArgumentException(ex);
        }
        return Collections.emptyMap();
    }

    protected abstract Map<String, Object> doFormat(Object srcObj);

    protected abstract Map<String, Object> doFormat(String text);

    protected abstract Map<String, Object> doFormat(File file);

    protected abstract Map<String, Object> doFormat(InputStream inputStream);

    protected abstract Map<String, Object> doFormat(URL url);
}
